package com.nananren.controller;

import com.aliyuncs.exceptions.ClientException;
import com.nananren.anno.OperateLogLog;
import com.nananren.common.Result;
import com.nananren.utils.AliOSSUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@CrossOrigin
@RestController
@RequestMapping("/common")
@Slf4j
public class uploadController {

    @Autowired
    private AliOSSUtils aliOSSUtils;

    @PostMapping
    @OperateLogLog
    public Result upload(MultipartFile file) throws IOException, ClientException {
        log.info("上传的文件为fileToString：{}", file.toString());
        log.info("上传的文件file为：{}", file);
        // 调用阿里云文件上传服务
        String url = aliOSSUtils.upload(file);

        // 把图片访问的url返回给前端
        return Result.success(null, url);
    }


    @PostMapping("/pic")
    public Result goPic(String pilePath) {
        log.info("pic为：{}", pilePath);

        return Result.success("上传成功", pilePath);
    }
}
